package com.cll.learn.filter;

import javax.servlet.ServletRequest;
import javax.xml.ws.Holder;

/**
 * 描述信息:
 * 数据保存到当前线程中。可以在web环境下获取
 * @author CLL
 * @version 1.0
 * @date 2020/10/9 16:13
 */
public class WebRequestHolder {

    /**
     * 全局的threadLocal对象。所有线程存储的servletRequest都在这里面
     * 内部维护的threadLocalMap包含了每个线程存储的变量
     */
    private static final ThreadLocal<ServletRequest> REQUEST_THREAD_LOCAL = new ThreadLocal<>();

    /**
     * 获取当前线程中存储的servletRequest对象
     * @return  servletRequest对象
     */
    public static ServletRequest getCurrentRequest() {
        return REQUEST_THREAD_LOCAL.get();
    }

    /**
     * 设置当前线程持有的request对象
     * @param request   servletRequest对象
     */
    static void settingServletRequest(ServletRequest request){
        REQUEST_THREAD_LOCAL.set(request);
    }

    /**
     * 移除当前线程持有的servletRequest对象
     */
    static void removeServletRequest() {
        REQUEST_THREAD_LOCAL.remove();
    }
}
